package com.lottery.im.util;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;

/**
 * Created by ztz on 2017/9/6.
 */
@SuppressLint("ObjectAnimatorBinding")
public class AnimTools {
  public static void anim(final View view, int startHeight, int endHeight, int duration) {
    ObjectAnimator anim =
        ObjectAnimator.ofInt(view, "view", startHeight, endHeight).setDuration(duration);
    anim.start();
    anim.addUpdateListener(valueAnimator -> {
      int cVal = (int) valueAnimator.getAnimatedValue();
      RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
      lp.height = cVal;
      view.setLayoutParams(lp);
    });
  }

  public static AnimationSet getAnimSet(float fromX, float toX) {
    AnimationSet animationSet = new AnimationSet(true);
    TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fromX,
        Animation.RELATIVE_TO_SELF, toX, Animation.RELATIVE_TO_SELF,
        0f, Animation.RELATIVE_TO_SELF, 0f);
    ta.setDuration(2000);
    animationSet.addAnimation(ta);
    animationSet.setFillAfter(true);
    return animationSet;
  }

  public static AnimationSet getAnimSet(float fromX, float toX, float fromY, float toY) {
    TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fromX,
        Animation.RELATIVE_TO_SELF, toX, Animation.RELATIVE_TO_SELF,
        fromY, Animation.RELATIVE_TO_SELF, toY);
    AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
    AnimationSet animationSet = new AnimationSet(true);
    animationSet.setFillAfter(true);
    animationSet.setDuration(3000);
    animationSet.addAnimation(ta);
    animationSet.addAnimation(alphaAnimation);
    return animationSet;
  }

  public static AnimationSet getAnimSet() {
    AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
    AnimationSet animationSet = new AnimationSet(true);
    animationSet.setFillAfter(true);
    animationSet.setDuration(3000);
    animationSet.addAnimation(alphaAnimation);
    return animationSet;
  }
}
